﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Marbles.Core.Settings;

namespace Marbles.Core.Content.SiteMap
{
    public class WebSiteMapNode
    {

        public bool HasChildren
        {
            get { return _childnodes.Count > 0; }
        }


        public string ParentPageName { get { return _parentpagename; } }
        public string Title { get { return _title; } }
        public string Description { get { return _description; } }
        public string PageName { get { return _pagename; } }
        public WebSiteMap ChildNodes { get { return _childnodes; } set { _childnodes = value; } }
        public string Url
        {
            get
            {
                if (SettingsManager.TrustLevel == AspNetHostingPermissionLevel.Medium)
                {
                    return string.Format("{0}://{1}/Default.aspx?id={2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, _pagename);
                }
                else
                {
                    return string.Format("{0}://{1}/{2}{3}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, _pagename, ".aspx");
                }
            }
        }
        public string LocalUrl
        {
            get
            {
                if (SettingsManager.TrustLevel == AspNetHostingPermissionLevel.Medium)
                {
                    return string.Format("/Default.aspx?id={0}", _pagename);
                }
                else
                {
                    return string.Format("{0}{1}", _pagename, ".aspx");
                }
            }
        }
        public DateTime PublishDate { get { return _publishdate; } }
        public bool Published { get { return _published; } }

        private WebSiteMap _childnodes = null;
        private string _parentpagename = string.Empty;
        private string _title = string.Empty;
        private string _description = string.Empty;
        private string _pagename = string.Empty;
        private WebSiteMapNode _parent = null;
        private DateTime _publishdate = DateTime.MinValue;
        private bool _published = false;

        public WebSiteMapNode(WebSiteMapNode parent, string parentpagename, int pageid, string title, string description, string pagename, DateTime publishdate, bool published)
        {
            _published = published;
            _publishdate = publishdate;
            _parent = parent;
            _parentpagename = parentpagename;
            _title = title;
            _description = description;
            _pagename = pagename;
            _childnodes = new WebSiteMap();
        }

        internal WebSiteMapNode(WebSiteMapNode parent, System.Data.DataRow row)
        {
            _parent = parent;
            _published = Convert.ToBoolean(row["published"]);
            _publishdate = Convert.ToDateTime(row["publishdate"]);
            _parentpagename = row["parentpagename"].ToString();
            _title = row["title"].ToString();
            _description = row["description"].ToString();
            _pagename = row["pagename"].ToString();
            _childnodes = new WebSiteMap();
        }
    }
}
